package MyDaily.dao.user;

/*添加用户方法*/
import java.sql.Connection;
import java.sql.PreparedStatement;

import MyDaily.dao.DBconnection;
import MyDaily.pojo.User;

public class InsertUser {
	// 新增数据
	public static int Insertuser(User user) {
		// 创建Connection
		Connection conn = null;
		// 创建PreparedStatement
		PreparedStatement pStat = null;
		try {
			// 定义sql语句
			String sql = "INSERT INTO `user`(phone,name,password,sex,birthday) VALUES(?,?,?,?,?)";
			// 获取连接
			conn = DBconnection.getConnection();
			// 编译sql
			pStat = conn.prepareStatement(sql);
			// 通过User实体类给sql语句赋值
			pStat.setString(1, user.getPhone());
			pStat.setString(2, user.getName());
			pStat.setString(3, user.getPassword());
			pStat.setString(4, user.getSex());
			pStat.setDate(5, user.getBirthday());
			// 执行sql，并返回执行结果
			return pStat.executeUpdate();
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			// 关闭资源
			try {
				if (pStat != null) {
					pStat.close();
				}

				if (conn != null) {
					conn.close();
				}
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
		return 0;
	}
}
